﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
using CommonsLib.DB ;
using MaxLeaf.Commons ;
using System.Data.Common;
using CommonsLib.Utility;
using CommonsLib.Settings; 


namespace MaxLeaf.DataFunction
{
    public class DataFunction
    {
        public string GetAndSetFinterID(FinterIDType fType,ConnectionInfo conFinterID)
        {
            DBAccess dbcon_FinterID = new DBAccess(conFinterID);
            long  lngNextFinterID = CvtFunc.ToLong (dbcon_FinterID.GetValue("select FNext from FInterID where Type="+ (int)fType +"")) ;
            string strSQL="";
            while (true)
            {

                strSQL = "Select FinterID from FInterIDHist where FinterID='" + lngNextFinterID + "' ";
                DbDataReader drFinter = dbcon_FinterID.GetDataFirstRow(strSQL);
                if (!drFinter.Read())
                {
                    try
                    {
                        strSQL = "Update FinterID set FinterID=FinterID+1";
                        dbcon_FinterID.ExecuteSql(strSQL);

                        strSQL = "Insert into FInterIDHist(FinterID,Type) values ({0},{1})";
                        strSQL = string.Format(strSQL, lngNextFinterID, (int)fType);
                        dbcon_FinterID.ExecuteSql(strSQL);
                        break;
                    }
                    catch (Exception ex)
                    {
                        lngNextFinterID++;
                    }
                }
                else
                {
                    lngNextFinterID++;
                }
                drFinter.Close();
            }
            return  CvtFunc.ToString (lngNextFinterID);
        }


    }
}
